<template>
  <div class="home">
    <Header />
    <div class='chartbody' id='IndexBody' >
      <div class="chartbox">
        <div ref='myChart'
             style="height: 100%"></div>
      </div>
      <div class="chartbox no-bor-b">
        <div ref='myChart1'
             style="height: 100%"></div>
      </div>
    </div>
  </div>
</template>
<script>
import Header from '@/components/Header.vue'
import { addWaterMarker } from './data'
import dataservice from '@/services/dataservice.js'
import chartData from './CharData' 
export default {
  data () {
    return {
      sumCharData1: [],
      sumCharData2: [],
      sumCharData3: [],
      dataTimeCreate: new Date()
    }
  },
  name: 'Index',
  components: {
    Header
  },
  mounted () {
    addWaterMarker('IndexBody', decodeURI(this.$cookies.get('UserName'))+this.dateTableFormat(this.dataTimeCreate,'yyyy-MM-dd'))
    this.initBuildingdatasummary().then(() => {
      this.initResources()
      this.initnoresources()
    })
  },
  methods: {
     // 创建用户访问记录
   async createdUserRecord () {
      let model = {}
      var name = 'Index' + this.$route.query.type
      model = {
        jm_channel: 20,
        jm_boardname: 'Index',
        jm_boardtype: this.$route.query.type,
        jm_address: window.location.href,
        jm_time: this.dateTableFormat(new Date(), 'yyyy-MM-dd'),
        ownerid: this.$cookies.get('UserId')
      }
      debugger
      let httpResult =await dataservice.createdUserRecord(model, model.ownerid)
      if (!httpResult) { console.log('查看APP端' + name + '错误') }
    },

    dateTableFormat (val, dateFormat) { 
      if (!val) {
        return ''
      }
      var date = new Date(val)  
      return dataservice.formatDate(new Date(val), dateFormat)
    },
    // 获取楼栋工程可售资源”、“取证可售资源
    async initBuildingdatasummary () {
      var entParam = {
        indicatorName: '',
        strWhere: '',
        type: 'sumMoney'
      }
      await dataservice.dataRouter('execute',
        {
          uri: 'jm_unifiedaction',
          entData: {
            functionname: 'accumulativemoeny_dashboard',
            params: JSON.stringify(entParam)
          },
          userid: this.$cookies.get('UserId') }).then(HttpResult => {
        if (HttpResult.status === 200 && HttpResult.data.res === 'success') {
          var results = JSON.parse(HttpResult.data.resData).statemessage
          if (results != '[]') {
            this.BuildingdatasummaryList = JSON.parse(results)[0] 
            let dataTimeCreate = this.BuildingdatasummaryList.datatimeCreate
            var date = new Date(dataTimeCreate) 
            //  date.setHours(date.getHours()-8) 
            // var getYear=date.getFullYear() 
            // var getMonth = date.getMonth()+1
            // if(getMonth.toString().length===1){getMonth='0'+getMonth}
            // var getDay=date.getDate()  
            this.dataTimeCreate=this.dateTableFormat(date.setMonth(date.getMonth()-1),'yyyy-MM')
          }
        }
      })
    },
    initResources () {
      var entParam = {
        indicatorName: '工程可售资源',  
        strWhere: " or jm_indicatorname='取证可售资源' and jm_level=20 ",
        type: 'chartReport'
      }

      dataservice.dataRouter('execute',
        {
          uri: 'jm_unifiedaction',
          entData: {
            functionname: 'accumulativemoeny_dashboard',
            params: JSON.stringify(entParam)
          },
          userid: this.$cookies.get('UserId') }).then(httpResult => {
        
        if (httpResult.status === 200 && httpResult.data.res === 'success') {
          var resData = JSON.parse(httpResult.data.resData).statemessage
          if (resData != '[]') {
            var result = JSON.parse(resData)
            result=result.sort((b,x)=>x.indexvalue-b.indexvalue)
             
            // 工程可售资源
            let jobResources = result.filter(item => {
              return item.jm_indicatorname === '工程可售资源'
            })
            // 取证可售资源
            let collectResources = result.filter(item => {
              return item.jm_indicatorname === '取证可售资源'
            })
            let newData=[]
            _.each(jobResources,x=>{
                  _.each(collectResources,item=>{
                    if(x.jm_areaidName===item.jm_areaidName){
                        newData.push(item.indexvalue)
                    }
                  })
            }) 
            let chartData1 = _.pluck(jobResources, 'indexvalue')
            let chartData2 =  newData
            let chartLabel1 = _.pluck(jobResources, 'jm_areaidName')
            let sumMoeny = _.pluck(result, 'indexvalue')
            // let sumCharData = _.reduce(sumMoeny, (memo, num) => { return memo + num }, 0)
            
            
            var myChart = this.$echarts.init(this.$refs.myChart)// this.sumCharData1+this.sumCharData2
            myChart.setOption(chartData.initChart5(chartLabel1, chartData1, chartData2, 0, this.dataTimeCreate))
          }
        }
      })
    },
    // 获取楼栋已获取未售资源
    initnoresources () {
      var entParam = {
        indicatorName: '已获取未售资源',
        strWhere: ' and jm_level=20 ',
        type: 'chartReport'
      }
      dataservice.dataRouter('execute',
        {
          uri: 'jm_unifiedaction',
          entData: {
            functionname: 'accumulativemoeny_dashboard',
            params: JSON.stringify(entParam)
          },
          userid: this.$cookies.get('UserId') }).then(httpResult => {
        if (httpResult.status === 200 && httpResult.data.res === 'success') {
          var resData = JSON.parse(httpResult.data.resData).statemessage
          if (resData != '[]') {
            var result = JSON.parse(resData)
            result=result.sort((b,x)=>x.indexvalue-b.indexvalue)
            let dataList = []
            // let sumCharData = 0
            for (let i = 0; i < result.length; i++) {
              let data = {
                name: result[i].jm_areaidName,
                value: result[i].indexvalue
              }
              // sumCharData += result[i].indexvalue
              dataList.push(data)
            } 
           let sumMoney= this.BuildingdatasummaryList.getresources
            var myChart1 = this.$echarts.init(this.$refs.myChart1)// this.sumCharData3
            debugger
            myChart1.setOption(chartData.initChart6(dataList, sumMoney, this.dataTimeCreate))
          }
        }
      })
    }
  },
  created(){
    debugger
      this.createdUserRecord ()
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.chartbox {
  width: 98%;
  margin: 0 auto;
  font-family: PingFangSC-Medium;
  // padding-bottom: 0.625rem;
}
.no-bor-b {
  border-bottom: 0 none;
}
</style>
